package com.senjoeson.androidtv.securitydemo;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MainActivity extends AppCompatActivity {


    private static final String TAG = "MainActivity:";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StringBuilder stringBuilder = new StringBuilder();
        TextView tv = (TextView) findViewById(R.id.sample_text);
        //  tv.setText(NativeLib.stringFromJNI());

        // CheckSecurityUtils.apkVerifyWithCRC(this,"");
        //  CheckSecurityUtils.apkVerifyWithMD5(this,"");
        //  CheckSecurityUtils.apkVerifyWithSHA(this,"");

        startService(new Intent(this, CheckBackgroundService.class));


        /*String source = "qwertyuisfdlsajdxcvnkhsakfh1332487";
        Log.d(TAG, "length: " + source.length());
        stringBuilder.append("当前未加密的字符串是:").append(source).append("\n");
        stringBuilder.append("加密中....").append("\n");
        String encodeAES = AesUtils.encodeAES(source);
        stringBuilder.append(encodeAES).append("\n");
        stringBuilder.append("下面开始解密....").append("\n");
        String decodeAES = AesUtils.decodeAES(encodeAES);
        stringBuilder.append("解密....").append("\n");
        stringBuilder.append(decodeAES).append("\n");
        tv.setText(stringBuilder.toString());

        //VisitManager.getInstance().init(this);

        String source1 = "我是一个中国人";

        String encode = AesUtils.base64Encode(source1);
        Log.d(TAG, "encode: " + encode);
        String decode = AesUtils.base64Decode(encode);
        Log.d(TAG, "decode: " + decode);

        VisitManager.getInstance().init(new VisitServerImpl());*/
    }


    public String getSHA1Signature(Context context) {
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);

            byte[] cert = info.signatures[0].toByteArray();

            MessageDigest md = MessageDigest.getInstance("SHA1");
            byte[] publicKey = md.digest(cert);
            StringBuilder hexString = new StringBuilder();
            for (int i = 0; i < publicKey.length; i++) {
                String appendString = Integer.toHexString(0xFF & publicKey[i])
                        .toUpperCase();
                if (appendString.length() == 1)
                    hexString.append("0");
                hexString.append(appendString);
                hexString.append(":");
            }
            return hexString.toString();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;

    }
}
